//
//  LoanCaculateUtil.m
//  LoanCalculate
//
//  Created by 冷悦 on 16/3/29.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "LoanCalculateUtil.h"
#import "CalculateLoanTableViewModel.h"

@implementation LoanCalculateUtil

#pragma mark - Private
- (float)calculateAverageCapitalRepayment{
    float repayment;
    
    if (self.amount > 0 && self.period > 0 && self.ratio > 0) {
        repayment = (self.amount * self.ratio * pow(1 + self.ratio, self.period)) /
        (pow(1 + self.ratio, self.period) - 1);
    }
    
    return repayment;
}

- (void)calculateAverageCapitalPlusInterest{
    NSMutableArray *paymentInfos = [NSMutableArray array];
    float totalRepayment = 0;
    float totalInterest = 0;
    float monthlyRepayment = [self calculateAverageCapitalRepayment];
    
    for (int i = 1; i < self.period + 1; i++) {
        float leftPrincipal = (pow(1 + self.ratio, self.period) - pow(1 + self.ratio, i)) /
        (pow(1 + self.ratio, self.period) - 1) * self.amount;
        float monthlyRepaymentInterest = leftPrincipal * self.ratio; //计算月利息
        float monthlyRepaymentPrincipal = monthlyRepayment - monthlyRepaymentInterest;
        float advanceRepayment = leftPrincipal + monthlyRepayment;
        totalRepayment += monthlyRepayment;
        totalInterest += monthlyRepaymentInterest;
        CalculateLoanTableViewModel *model = [CalculateLoanTableViewModel new];
        model.number = [NSString stringWithFormat:@"%d", i] ;
        model.monthlyRepayment = [NSString stringWithFormat:@"%f", monthlyRepayment];
        model.monthlyRepaymentPrincipal = [NSString stringWithFormat:@"%f", monthlyRepaymentPrincipal];
        model.monthlyRepaymentInterest = [NSString stringWithFormat:@"%f", monthlyRepaymentInterest];
        model.leftPrincipal = [NSString stringWithFormat:@"%f", leftPrincipal];
        model.advanceRepayment = [NSString stringWithFormat:@"%f", advanceRepayment];
        [paymentInfos addObject:model];
    }
    
    _paymentInfos = paymentInfos;
    _totalInterest = totalInterest / 10000;
    _averagePayMoneyPerMonth = totalRepayment / _period;
    _totalRepayment = totalRepayment / 10000;
}

- (void)calculateAverageCapital{
    NSMutableArray *paymentInfos = [NSMutableArray array];
    float leftPrincipal = self.amount;
    float totalRepayment = 0;
    float totalInterest = 0;
    
    for (int i = 1; i < self.period + 1; i++) {
        float monthlyRepaymentPrincipal = (float)self.amount / self.period;
        float monthlyRepaymentInterest = leftPrincipal * self.ratio;
        float monthlyRepayment = monthlyRepaymentPrincipal + monthlyRepaymentInterest;
        float advanceRepayment = leftPrincipal + monthlyRepaymentInterest;
        leftPrincipal = leftPrincipal - monthlyRepaymentPrincipal;
        totalRepayment += monthlyRepayment;
        totalInterest += monthlyRepaymentInterest;
        CalculateLoanTableViewModel *model = [CalculateLoanTableViewModel new];
        model.number = [NSString stringWithFormat:@"%d", i] ;
        model.monthlyRepayment = [NSString stringWithFormat:@"%f", monthlyRepayment];
        model.monthlyRepaymentPrincipal = [NSString stringWithFormat:@"%f", monthlyRepaymentPrincipal];
        model.monthlyRepaymentInterest = [NSString stringWithFormat:@"%f", monthlyRepaymentInterest];
        model.leftPrincipal = [NSString stringWithFormat:@"%f", leftPrincipal];
        model.advanceRepayment = [NSString stringWithFormat:@"%f", advanceRepayment];
        [paymentInfos addObject:model];
    }
    
    _paymentInfos = paymentInfos;
    _totalInterest = totalInterest / 10000;
    _averagePayMoneyPerMonth = totalRepayment / _period;
    _totalRepayment = totalRepayment / 10000;
}

- (void)calculateTotalLoan{
    _disccountMoney = _houseTotalPrice * _discountRatio;
    _amount = _houseTotalPrice - _disccountMoney;
    _firstPayment = _amount * _firstPaymentRatio;
    _loanTotalMoney = _amount - _firstPayment;
    //上面精确到万元，下面计算为元，所以转化单位
    _amount = _loanTotalMoney * 10000;
}

#pragma mark - Public
- (void)calculate{
    [self calculateTotalLoan];
    if (self.loanCalculateType == AverageCapital) {
        [self calculateAverageCapital];
    } else {
        [self calculateAverageCapitalPlusInterest];
    }
}

@end
